package com.example.zookeeper1.demos;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;


public class DataWatch implements Watcher {

    private ZooKeeper zk;
    private String path;

    public DataWatch(ZooKeeper zk, String path) {
        this.zk = zk;
        this.path = path;
    }
    public void watchDataChange() throws InterruptedException, KeeperException {
        byte[] data = zk.getData(this.path, this, null);
        String dataString = new String(data);
        System.out.println("data change============:" + dataString);
    }


    @Override
    public void process(WatchedEvent event) {
        try {
            this.watchDataChange();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (KeeperException e) {
            throw new RuntimeException(e);
        }
    }
}
